package com.example.window;

import com.example.model.WaterSensor;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * ClassName: FullWindowFunction
 * Package: com.example.window
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-07-23
 * Time: 15:30
 */

//全窗口函数
public class FullWindowFunction {
    public static void main(String[] args) throws Exception {
        //全窗口函数有两个 process 和 apply方法
        //但是 process的功能可以完全替代 apply方法
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();


        SingleOutputStreamOperator<WaterSensor> map = env.socketTextStream("hadoop103", 7777)
                .map(new MapFunction<String, WaterSensor>() {
                    @Override
                    public WaterSensor map(String value) throws Exception {
                        String[] s = value.split(" ");

                        return new WaterSensor(s[0], Long.valueOf(s[1]), Integer.valueOf(s[2]));
                    }
                });
        KeyedStream<WaterSensor, String> key = map.keyBy(value -> value.getId());


        //开窗 滚动处理时间窗口
        key.window(TumblingProcessingTimeWindows.of(Time.seconds(10)))
                //参数分别表述 输入类型 输出类型 key的类型 窗口类型
                .process(new ProcessWindowFunction<WaterSensor, String, String, TimeWindow>() {
                    /**
                     * process 全窗口函数
                     * @param s key的类型
                     * @param context 上下文
                     * @param elements 进来窗口的全部数据
                     * @param out 采集器 输出用的
                     * @throws Exception
                     */
                    @Override
                    public void process(String s, Context context, Iterable<WaterSensor> elements, Collector<String> out) throws Exception {
                        //通过上下文 拿到窗口的启停时间
                        final long start = context.window().getStart();
                        final long end = context.window().getEnd();
                        final String st = DateFormatUtils.format(start, "yyyy-MM-dd HH:mm:ss");
                        final String en = DateFormatUtils.format(end, "yyyy-MM-dd HH:mm:ss");

                        //获取条数
                        final long l = elements.spliterator().estimateSize();

                        out.collect("key=" + s + " 的窗口的时间[ " + st + "," + en + "] 包含多少条数据: " + l);


                    }
                }).print();

        env.execute();
    }
}
